package com.etcbase.dcs.controller.map;

import java.util.ArrayList;

import android.graphics.drawable.Drawable;
import android.view.MotionEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MapMarkersOverlay extends ItemizedOverlay<OverlayItem> {
	public static final String TAG = "MarkersOverlay";

	private ArrayList<OverlayItem> overlayItemsList;
	private ArrayList<Boolean> markerIsFreeList;
	private OverlayItem selectedOverlayItem = null;

	private Drawable defaultMarker;
	private Drawable selectedMarker;
	private Drawable invisibleMarker;

	// CONSTRUCTOR
	public MapMarkersOverlay(Drawable defaultMarker, Drawable selectedMarker,
			Drawable invisibleMarker) {
		// default marker is set to the saved-location-marker since they are
		// created more often
		super(boundCenterBottom(defaultMarker));
		this.defaultMarker = boundCenterBottom(defaultMarker);
		this.selectedMarker = boundCenterBottom(selectedMarker);
		this.invisibleMarker = boundCenter(invisibleMarker);

		markerIsFreeList = new ArrayList<Boolean>();
		overlayItemsList = new ArrayList<OverlayItem>();

		// add a dummy invisible marker to avoid any crashes (stupid but
		// necessary to avoid any crashes on android 4.0.3)
		OverlayItem overlayItem = new OverlayItem(new GeoPoint(0, 0), "", "");
		overlayItem.setMarker(invisibleMarker);
		int index = getFreeSlot();
		overlayItemsList.set(index, overlayItem);
		markerIsFreeList.set(index, false);
		populate();
	}

	public void addSavedLocationMarker(GeoPoint p) {
		addSavedLocationMarker(p, null, null);
	}

	public void addSavedLocationMarker(GeoPoint p, String title, String snippet) {
		// perform reusable deletion:
		// 1) set an invisible (fully transparent) marker
		// 2) mark the overlay-item as free
		OverlayItem overlayItem = new OverlayItem(p, title, snippet);
		overlayItem.setMarker(defaultMarker);
		int index = getFreeSlot();
		overlayItemsList.set(index, overlayItem);
		markerIsFreeList.set(index, false);
		populate();
	}

	// REMEMBER: to see changes you have to call populate() !
	// populate() is not called here, because you may want to delete multiple
	// items and then see the changes !
	public void freeItem(int index) {
		markerIsFreeList.set(index, true); // mark this item as free
		overlayItemsList.get(index).setMarker(invisibleMarker);
	}

	public void freeAllSavedLocationMarkers() {
		for (int i = 1; i < overlayItemsList.size(); i++) {
			markerIsFreeList.set(i, true);
			overlayItemsList.get(i).setMarker(invisibleMarker);
		}
	}

	public int getFreeSlot() {
		// start searching from the first one
		for (int i = 0; i < markerIsFreeList.size(); i++) {
			if (markerIsFreeList.get(i)) {
				return i;
			}
		}

		// create a new slot
		overlayItemsList.add(null);
		markerIsFreeList.add(false);
		return overlayItemsList.size() - 1;
	}

	public int getNumberOfFreeItems() {
		int numFree = 0;
		for (int i = 0; i < markerIsFreeList.size(); i++) {
			if (markerIsFreeList.get(i))
				numFree++;
		}
		return numFree;
	}

	public OverlayItem getSelectedOverlayItem() {
		return selectedOverlayItem;
	}

	public boolean setSelectedOverlayItem(int index) {
		if (!isValidOverlayItem(index)) {
			return false;
		}

		if (selectedOverlayItem != null) {
			selectedOverlayItem.setMarker(defaultMarker);
		}
		selectedOverlayItem = overlayItemsList.get(index);
		selectedOverlayItem.setMarker(selectedMarker);
		populate();
		return true;
	}

	public boolean isValidOverlayItem(int index) {
		// invalid if it is dummy item OR a free item (both invisible)
		if ((index == 0) || markerIsFreeList.get(index)) {
			return false;
		}
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		return super.onTouchEvent(event, mapView);
	}

	// TAP-EVENTS
	public interface OnTapListener {
		public void onTap(int index);
	}

	public void setOnTapListener(OnTapListener listener) {
		mOnTapListener = listener;
	}

	private OnTapListener mOnTapListener;

	@Override
	protected boolean onTap(int index) {

		if (mOnTapListener != null) {
			mOnTapListener.onTap(index);
		}

		return super.onTap(index);
	}

	@Override
	protected OverlayItem createItem(int i) {
		if (overlayItemsList.size() <= i) {
			for (int j = overlayItemsList.size(); j <= i; j++)
				overlayItemsList.add(new OverlayItem(null, null, null));
		}

		return overlayItemsList.get(i);
	}

	@Override
	public int size() {
		return overlayItemsList.size();
	}
}
